#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCryptographicHash>
#include <QFile>
#include <QApplication>
#include <QPropertyAnimation>
#include <QDesktopWidget>
#include <QEasingCurve>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    on_checkBox_time_clicked();
    on_checkBox_count_clicked();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    QString serial = ui->lineEdit_serial->text();
    if(serial.isEmpty())
    {
       showHintLabel(nullptr,"请输入序列号后再执行此操作！");
       return;
    }
    QFile publicKey(":/new/prefix1/publicKey.p");
    if(!publicKey.open(QIODevice::ReadOnly))
    {
        showHintLabel(nullptr,"公钥获取失败！");
        return;
    }
    QByteArray keyData = publicKey.readAll();
    QString cacul_Register = caculateRegister(serial,keyData);
    ui->lineEdit_register->setText(cacul_Register);
}

QString MainWindow::caculateRegister(QString serial,QByteArray keyData)
{
    QByteArray hashData = QCryptographicHash::hash(keyData,QCryptographicHash::Md5);
    hashData += serial.toLocal8Bit();
    QByteArray hashData2 = QCryptographicHash::hash(hashData,QCryptographicHash::Md5);
    QByteArray lastData;
    for(int i = 0;i < hashData2.size()/2;i ++)
    {
        char buff = hashData2.at(2*i)*hashData.at(2*i) + hashData2.at(2*i+1)*hashData.at(2*i+1);
        lastData.append(buff);
    }
    QString caculateRegister = HexToHexstr(lastData).remove(QRegExp("\\s"));
    return caculateRegister;
}

/*******************************************
时间：2019-4-22 10点42分
作者：Kiang Tao
功能：HEX数组转HEX字符串
输入参数：hex数组  如：01 ff 0f f0
返回值：hex字符串  如："01 ff 0f f0"
*******************************************/
QString MainWindow::HexToHexstr(const QByteArray hex)
{
    //QString::number(hex,16);
    if(hex.isEmpty())   return "";
    QByteArray DataIn = hex;
    QString HexStr;
    QDataStream DataStream(&DataIn,QIODevice::ReadWrite);
    while(!DataStream.atEnd())
    {
        quint8 outChar = 0;
        DataStream >> outChar;
        HexStr += QString("%1").arg(outChar & 0xff,2,16,QLatin1Char('0'))+" ";
    }
    return HexStr.toUpper();    //换大写字母
}

void MainWindow::on_pushButton_cance_clicked()
{
    close();
}

void MainWindow::on_checkBox_time_clicked()
{
    ui->lineEdit_time->setHidden(!ui->checkBox_time->isChecked());
    ui->label_timeUint->setHidden(!ui->checkBox_time->isChecked());
}

void MainWindow::on_checkBox_count_clicked()
{
    ui->lineEdit_count->setHidden(!ui->checkBox_count->isChecked());
    ui->label_countUint->setHidden(!ui->checkBox_count->isChecked());
}


void MainWindow::showHintLabel(QWidget *parentWin, QString strText, QString strFontSize, QString strColor, QString strBgColor)
{
    if(nullptr == parentWin)
        parentWin = QApplication::desktop()->screen();

    QFrame *pFrame = new QFrame(parentWin);        //为了兼容parent为nullptr时的圆角边框  方法是背景透明 上边叠加圆角控件
    QLabel *pLabel = new QLabel(pFrame);    //创建显示标签
    pLabel->setStyleSheet(QString("QLabel{background:%1;color:%2;font:%3 SimHei;border-radius:5px;}")
                              .arg(strBgColor).arg(strColor).arg(strFontSize)); //社长标签风格
    pLabel->setText(strText);                   //设置文字内容
    pLabel->setAlignment(Qt::AlignCenter);      //文字居中显示
    pLabel->adjustSize();
    pLabel->resize(pLabel->size() + QSize(120,120));

    pFrame->resize(pLabel->size());                     //设置大小为label大小
    pFrame->setWindowFlags(Qt::FramelessWindowHint);    //去除外框
    pFrame->setAttribute(Qt::WA_TranslucentBackground);
    //pFrame->move((parent->width()-pFrame->width())/2,(parent->height()-pFrame->height())/2);
    pFrame->show();

    QPropertyAnimation *pAnimation = new QPropertyAnimation(pFrame,"windowOpacity",this);
    pAnimation->setDuration(2000);
    pAnimation->setEasingCurve(QEasingCurve::InCirc);
    pAnimation->setStartValue(1.0);
    pAnimation->setEndValue(0.0);
    pAnimation->start();        //启动动画
    connect(pAnimation,&QPropertyAnimation::finished,[=]{   //动画结束后删除创建的资源
        delete pAnimation;
        delete pFrame;
    });
}

